<template>
	<view class="class-detail">
		<view class="header-box">
			<view class="back-btn" @click="goback">
			  <text class="cuIcon cuIcon-back text-white"></text>
			</view>
		<view class="title"> 班级学生管理 </view>
		</view>
		<view class="class-info">
					<view class="class-title"> 班级信息详情 </view>
					<text class="class-name">班级名称 : {{ classInfo.class_name }}</text>
					<text class="student-total">班级学生总数 : {{ allstudentInfo.length }} 人</text>
					
					<!-- 新增考勤按钮 -->
					<view 
						class="attendance-btn"
						:class="{ teacher: userType === 2 }"
						@click="handleAttendance"
						>
						{{ userType === 2 ? '考勤管理' : '学生考勤' }}
						</view>
				</view>
		<scroll-view scroll-y="true" class="class-student">
			<view class="student-item" 
			v-for="(item,index) in allstudentInfo" 
			:key="item.studentId" v-if="item.status===1" 
			@click="goToStudentDetail(item.studentId)">
				<view class="student-avatar">
					<image src="@/static/images/avatar.png" mode=""></image>
				</view>
				<view class="student-info">
					<text class="student-name"> {{item.studentName}} </text>
					<text class="student-id">{{item.studentId}}</text>
				</view>
			</view>
		</scroll-view>
			
		<wButton
		    class="add-student"
		    text='添加学生'
		    @click="addstudent"
		    bgColor="#1890FF">
		</wButton>
		
		<!-- 添加学生 -->
		<popup :show="isshow" @close="closepopup" @confirm="consfirmadd" title="添加学生">
			<input type="text" class="inputstudentid" v-model="addstudentid" placeholder="请输入学生的学号"/>
			<input type="text" class="inputname" v-model="addstudentname" placeholder="请输入学生的姓名"/>
			<radio-group name="radio" @change="(e)=>addstudentstatus = parseInt(e.detail.value)" class="radio">
				<label>
					<radio value="0" :checked="addstudentstatus === 0" /><text>普通学生</text>
				</label>
				<label>
					<radio value="1" :checked="addstudentstatus === 1" /><text>班委</text>
				</label>
			</radio-group>
		</popup>
	</view>
</template>

<script>
	import popup from '../../components/popup/popup.vue'
	import wButton from '../../components/watch-login/watch-button.vue'
	import { addStudent , selectAllStudent } from '../../api/class'
	export default {
		data() {
			return {
				userType: 2, 
				classInfo:{}, //班级信息
				isshow:false,
				addstudentid:'',
				addstudentname:'',
				addstudentstatus:0,
				addstudentInfo:{
					id: 0,  //班级ID号
					className: "",
					studentId: "",
					studentName: "",
					teacherId:"" ,
					teacherName: "",
					student_status: 0,
					status: 0 
				},
				//模拟的数据
				allstudentInfo: [{
                   studentId: "2023535232361",
                   studentName: "张三",
                   gender: 1,
                   age: 18,
                   address: "北京市海淀区",
                   phoneNumber: "13800138001",
                   familyNumber: "010-12345678",
                   absence: 2,
                   studentStatus: 1, // 1-在读
                   status: 1 // 1-正常
                 },
                 {
                   studentId: "2023535232362",
                   studentName: "李四",
                   gender: 1,
                   age: 19,
                   address: "上海市浦东新区",
                   phoneNumber: "13900139002",
                   familyNumber: "021-87654321",
                   absence: 0,
                   studentStatus: 1,
                   status: 1
                 }]
			}
		},
		components:{ popup , wButton},
		onLoad(options) {
			if(!options.classitem) return 
			const res=JSON.parse(options.classitem)
			this.classInfo=res
			console.log(this.classInfo)
			this.addstudentInfo.className=res.class_name
			this.addstudentInfo.id=res.id
		},
		onShow() {
			this.getclassstudents()
			this.checkLoginStatus()
		},
		methods: {
			goback(){
				uni.navigateBack()
			},
			
			// 检查登录状态
			checkLoginStatus() {
							const userInfo = uni.getStorageSync('userInfo')
							if (!userInfo) {
								uni.redirectTo({ url: '/pages/login/login' })
								return
							}
							this.userType = 2 // 假设接口返回userType字段
							this.addstudentInfo.teacherName = userInfo.name
							this.addstudentInfo.teacherId = userInfo.teacherId
						},
			//汇总班级的学生
			async getclassstudents(){
				uni.showLoading({ title:'获取班级学生中...'})
				try{
					const res = await selectAllStudent({classId: this.classInfo.id })
					if(res.code===200){
						uni.showToast({
							title:res.message||'获取成功',
							icon:'none',
							duration:2000
						})
						this.allstudentInfo = res.data
					}
				}catch(error){
					uni.showToast({
						title:error.message||'获取失败',
					    icon:'none',
						duration:2000
					})
				}finally{
					uni.hideLoading()
				}
			},
			
			//点击跳转到学生详情
			goToStudentDetail(id){
				uni.navigateTo({
					url:`/pages/studentdetail/studentdetail?studentId=${id}&classid=${this.classInfo.id}`,
				})
			},
			
			//点击添加学生按钮
			addstudent(){
				this.isshow=true
			},
			
			//校验
			validateInput(){
				if (!/^\d{13}$/.test(this.addstudentid)) {
					uni.showToast({ title: '学号应为13位数字', icon: 'none' })
					return false
				}
				if (!this.addstudentname || this.addstudentname.trim().length < 2) {
				    uni.showToast({ title: '请输入正确的姓名', icon: 'none' })
					return false
				}
				return true
			},
			// 新增考勤处理方法
						handleAttendance() {
							if(this.userType === 2) {
								// 教师进入考勤管理
								uni.navigateTo({
									url: `/pages/attendance/manage`//?classId=${this.classInfo.id}
								})
							} else {
								// 学生进入考勤页面
								uni.navigateTo({
									url: `/pages/attendance/checkin`//?classId=${this.classInfo.id}
								})
							}
						},
			//点击确认添加
			async consfirmadd(){
				if(!this.validateInput()) return
				this.addstudentInfo.studentId=this.addstudentid
				this.addstudentInfo.studentName=this.addstudentname
				this.addstudentInfo.student_status=this.addstudentstatus
				console.log(this.addstudentInfo)
				uni.showLoading({ title: '添加学生中...' })
				try{
					const res = await addStudent([this.addstudentInfo])
					if(res.code===200){
						uni.showToast({
							title:res.message||'添加成功',
							icon:'none',
							duration:2000
						})
					    this.getclassstudents();
					}
				}catch(error){
					uni.showToast({
						title:error.message||'添加失败',
					    icon:'none',
						duration:2000
					})
				}finally{
					uni.hideLoading()
					this.closepopup();
				}
			},
			
			closepopup(){
				this.isshow=false
				this.addstudentid=''
				this.addstudentname=''
				this.addstudentstatus=0
			}
		}
	}
</script>

<style>
	@import url("../../components/watch-login/css/icon.css");
	@import url("css/classdeatil.css");
</style>
